-- UIBossStone
-- Created by dengc Sep/2/2016
-- 圣者之塔BOSS层石柱界面

UIBossStone = class("UIBossStone", function()
    return cc.Layer:create();
end);

function UIBossStone.create(pos)
    return UIBossStone.new(pos);
end

-- 构造函数
function UIBossStone:ctor(pos)
    self:setName("UIBossStone");
    local node = cc.CSLoader:createNode("layout/stone/MagicStone.csb");
    self:addChild(node);
    self.node = node;

    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    -- 记录石柱信息
    local gridData = DungeonM.getGridByPos(pos);
    self.pos = pos;
    self.gridData = gridData;
    self.stoneId  = self.gridData.stone_id;

    -- 注册点击事件
    self:registerTouchEvent()

    -- 注册事件回调处理函数
    self:registerEventCallback();

    -- 重绘
    self:redraw();

    -- 适配
    self:resize();
end

-- 适配
function UIBossStone:resize()
    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 重绘
function UIBossStone:redraw()
    local descLabel  = findChildByName(self.node, "CT/desc");
    local iconImg    = findChildByName(self.node, "CT/icon");
    local titleLabel = findChildByName(self.node, "CT/title_node/title");
    local btnOk      = findChildByName(self.node, "CT/btn_ok");
    local kerning, kerning2 = 0, 0;
    if getLang() ~= "zh" and getLang() ~= "tw" then
        kerning, kerning2 = -3, -1;
    end

    local stoneId = self.stoneId;
    local stoneType = MagicStoneM.query(stoneId, "type");
    local dialog    = MagicStoneM.query(stoneId, "dialog");
    local iconName  = MagicStoneM.getStoneIcon(stoneId);
    local iconPath  = getLevelItemIconPath(iconName);
    local title;
    local desc;
    local okBtnText;

    -- 消耗，需要在已关闭的石柱上动态显示
    local cost = MagicStoneM.query(stoneId, "closed_cost");

    if stoneType == STONE_TYPE_LIGHT then
        -- 光之石柱
        title = getLocStr("light_magic_stone_title");
        desc = getLocStr("unclose_light_magic_stone_desc");
        okBtnText = getLocStr("btn_text_close_stone");
    else
        -- 暗之石柱
        title = getLocStr("dark_magic_stone_title");
        desc = getLocStr("unclose_dark_magic_stone_desc");
        okBtnText = getLocStr("btn_text_close_stone");
    end

    TextStyleM.setTextStyle(btnOk, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    TextStyleM.setArtWordTitleStyle(titleLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN, false, kerning2);

    titleLabel:setString(title);
    iconImg:loadTexture(iconPath);
    btnOk:setTitleText(okBtnText);

    iconImg:setScale(1.2);

    applyString(descLabel, desc);

    -- 显示消耗
    self:redrawCost(cost);
end

-- 重绘消耗
function UIBossStone:redrawCost(cost)
    local classId = table.keys(cost)[1];
    local needAmount = table.values(cost)[1];
    local curAmount = ItemM.getAmount(ME.user, classId);

    -- 显示材料图标
    local costNode = findChildByName(self.node, "CT/cost_node");
    local iconImg = findChildByName(costNode, "icon");
    local iconPath = getItemIconPath(ItemM.query(classId, "icon"));
    iconImg:loadTexture(iconPath);

    -- 显示数量信息
    local amountLabel = findChildByName(costNode, "amount");

    -- 消耗不足时，显示红色
    if curAmount < needAmount then
        TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_RED);
    else
        TextStyleM.setTextStyle(amountLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BEIGE);
    end
    amountLabel:setString(curAmount .. "/" .. needAmount);
end

-- 注册点击事件
function UIBossStone:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");

            -- 关闭当前界面
            UIMgr.getCurrentScene():removeFormByName("UIBossStone");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 注册确定按钮点击事件
    local btnOk = findChildByName(self.node, "CT/btn_ok");
    local function onOkClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 判断能否关闭石柱
            local ret = SaintTacticsM.checkClosed(self.pos);
                if ret ~= true then
                    -- 材料不足
                    local cost = MagicStoneM.query(self.stoneId, "closed_cost");
                    local classId = table.keys(cost)[1];
                    local name = ItemM.query(classId, "name");
                    local msg = string.format(getLocStr("magic_stone_operate_fail"), name);
                    alert(msg);
                    return;
                end

                -- 关闭石柱
                DungeonActionM.go("close_boss_stone", self.pos, self.stoneId);

            -- 限制点击频率
            setClickFrequency(sender, 1);
        end
    end
    btnOk:addTouchEventListener(onOkClick);
end

-- 注册事件回调处理函数
function UIBossStone:registerEventCallback()
    -- 关注石柱关闭的事件
    EventMgr.register("UIBossStone", event.UPDATE_STONE_STATUS, function(args)
        local status = args.status;
        if status ~= STONE_STATE_CLOSED then
            return;
        end

        -- 提示玩家
        local stoneId = args.id;
        local stoneType = MagicStoneM.query(stoneId, "type");
        if stoneType == STONE_TYPE_LIGHT then
            alert(getLocStr("close_light_magic_stone_ok"));
        else
            alert(getLocStr("close_dark_magic_stone_ok"));
        end

        -- 关闭当前界面
        UIMgr.getCurrentScene():removeFormByName("UIBossStone");
    end);

    self:registerScriptHandler(function(eventType)
        if eventType == "exit" then
            cclog("UIBossStone 界面析构清理");

            EventMgr.removeAll("UIBossStone");

            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():showMenu();
            end
        elseif eventType == "enter" then
            if UIDungeonMgr:getCurLevel() then
                UIDungeonMgr:getCurLevel():hideMenu();
            end
        end
    end);
end
